package com.zs4sdk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zs4gamesdk.R;
import com.zs4sdk.Zs4GameBase;
import com.zs4sdk.Zs4GameMenu;
import com.zs4sdk.activity.resetPassword.Zs4GameResetPasswordActivity;
import com.zs4sdk.common.Extra;
import com.common.library.data.CommonEventHandler.ResultHandlerInEvent;
import com.common.library.data.CommonHandle;
import com.common.library.data.CommonHandle.HandleMessage;
import com.common.library.data.EventHandler2Get;
import com.zs4sdk.entity.LoginJSONResultEntity;
import com.common.library.util.ActivityHelper;
import com.common.library.util.EncryptHelper;
import com.common.library.util.InputMethodeUtil;
import com.common.library.util.NetworkHelper;
import com.common.library.util.ParamsHelper;
import com.common.library.widget.SDKGameClearEditText;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * function: 找回密码
 *
 * @ author: aXiBa
 */
public class Zs4GameForgetPasswordActivity extends Zs4GameBaseActivity implements OnClickListener {

    public static final String TAG = Zs4GameForgetPasswordActivity.class.getSimpleName();

    LinearLayout verifyAccountLinearLayout;
    LinearLayout unbindAccountLinearLayout;
    SDKGameClearEditText inputPhoneOrNameEditText;
    Button VerifyAccountButton;
    Button connectSerButton;
    TextView gotoLoginByPhoneTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ActivityHelper.getLayoutResId("zs4gamelayout_act_forget_password"));
        init();
    }

    @Override
    protected void init() {
        super.init();
        setTopTitle(ActivityHelper.getStringResId("zs4gamestring_text_forget_code"));

        initReferences();

        setViewClickEvent(this, ActivityHelper.getIdResId("zs4gameviewid_forget_act_btn_do_next_step"),
                ActivityHelper.getIdResId("zs4gameviewid_forget_act_goto_login_by_phone"),
                ActivityHelper.getIdResId("zs4gameviewid_forget_act_btn_to_kefu"));

    }


    /**
     * 提交手机找回密码
     */
    private void confirmPhone() {
        InputMethodeUtil.closeInputMethod(this);

        String nickname = inputPhoneOrNameEditText.getText().toString().trim();
        if(nickname.isEmpty()){
            return;
        }

        if (NetworkHelper.isNetworkAvailable(this)) {

            ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
            paramList.add(new BasicNameValuePair("account", EncryptHelper.encryptByAES(nickname)));
//            paramList.add(new BasicNameValuePair("sendmode", sendmode));

            new EventHandler2Get(
                    Extra.URL.ACCOUNTTYPE,
                    paramList,
                    ParamsHelper.getUserAgent1(), new ResultHandlerInEvent() {

                @Override
                public void doResultHanler(JSONObject result) throws JSONException {

                    Message msg = myHandle.obtainMessage();

                    msg.what = Extra.MessageKey.REQUEST_VERIFY_SUCCESS;
                    msg.obj = result;

                    Bundle bundle = new Bundle();
//
                    msg.setData(bundle);

                    // 发送消息
                    myHandle.sendMessage(msg);

                }

                @Override
                public void doExceptionHandler() {

                }
            });
        } else {
            ActivityHelper.showToast(ActivityHelper.getStringResId("zs4gamestring_hint_has_not_network"));
        }

    }


    CommonHandle myHandle = new CommonHandle(Zs4GameForgetPasswordActivity.this, new HandleMessage() {

        @Override
        public void doHandleMessage(Message msg) {

            if (Extra.MessageKey.REQUEST_VERIFY_SUCCESS == msg.what) {
                LoginJSONResultEntity entity = new LoginJSONResultEntity((JSONObject) msg.obj);
                if (entity.getResult() == Extra.RESULTCODE.SUCCESS) {
                    if(entity.getVerify().equals("1")){
                        Intent intent = new Intent(Zs4GameForgetPasswordActivity.this, Zs4GameRetrievePasswordActivityBase.class);
                        startActivity(intent);
                        finish();
                    }else if(entity.getVerify().equals("0")){
                        ActivityHelper.showToast(entity.getDesc());

                    }else if(entity.getVerify().equals("2")){
                        showUNBindAccount();
                    }

                }else {
                    ActivityHelper.showToast(entity.getDesc());
                }
            }


        }
    });

    @Override
    public void onClick(View v) {
        super.onClick(v);
        //下一步
        if (ActivityHelper.getIdResId("zs4gameviewid_forget_act_btn_do_next_step") == v.getId()) {
//            gotoVerifyPhone();
            confirmPhone();

        }// 手机验证码登录
        else if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_forget_act_goto_login_by_phone")) {
//            getVerifyCode(Extra.VERIFYTYPE.VERIFYTYPE_TEXTMESSAGE);
            Intent mIntent = new Intent();
            mIntent.putExtra("login", "login");
            this.setResult(Extra.REQUESTCODE.REQUESTRetrievePassword, mIntent);
            finish();

        }
        // 联系客服
        else if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_forget_act_btn_to_kefu")) {
            String clientStr = Zs4GameForgetPasswordActivity.this.getString(R.string.online_customer);
            Zs4GameMenu.showWebView(this,
                    clientStr,
                    Extra.URL.SDKGAME_FEEDBACK_FIND_ACCOUNT,
                    null,
                    "3");
        }
    }

    /**
     * 显示未绑定提示
     */
    private void showUNBindAccount() {
        verifyAccountLinearLayout.setVisibility(View.GONE);
        unbindAccountLinearLayout.setVisibility(View.VISIBLE);
    }

    private void gotoVerifyPhone() {

        Bundle bundle = new Bundle();
        bundle.putInt(Extra.INTENTKEY.SCREEN_ORIENTATION, this.mScreenOrientation);
        // 根据类型，来控制页面显示
        bundle.putInt(Extra.INTENTKEY.TITLE, Extra.REQUESTCODE.RESETPASSWORD);
        bundle.putString(Extra.INTENTKEY.FLAG, this.flag);
        Intent intent = new Intent(this, Zs4GameResetPasswordActivity.class);
        intent.putExtras(bundle);
        startActivityForResult(intent, Extra.REQUESTCODE.REQUESTRetrievePassword);

    }

    // 设置界面
    private void initReferences() {

        verifyAccountLinearLayout = findView(LinearLayout.class, ActivityHelper.getIdResId("zs4gameviewid_forget_act_lin_verify_account"));
        unbindAccountLinearLayout = findView(LinearLayout.class, ActivityHelper.getIdResId("zs4gameviewid_forget_act_lin_nubind_account"));
        inputPhoneOrNameEditText = findView(SDKGameClearEditText.class, ActivityHelper.getIdResId("zs4gameviewid_forget_act_edt_input_phone_or_name"));
        VerifyAccountButton = findView(Button.class, ActivityHelper.getIdResId("zs4gameviewid_forget_act_btn_do_next_step"));
        connectSerButton = findView(Button.class, ActivityHelper.getIdResId("zs4gameviewid_forget_act_btn_to_kefu"));
        gotoLoginByPhoneTextView = findView(TextView.class, ActivityHelper.getIdResId("zs4gameviewid_forget_act_goto_login_by_phone"));
        if(Zs4GameBase.getInstance().isLogined()){
            gotoLoginByPhoneTextView.setVisibility(View.GONE);
        }else {
            gotoLoginByPhoneTextView.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected  int getLeftBtnType() {
        return TYPE_LEFTBTN_BACK;
    }

    @Override
    protected  int getRightBtnType() {
        return TYPE_RIGHTBTN_INVISIBLE;
    }
}
